package cn.iocoder.devlop.module.annual.report.aspect;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.devlop.module.annual.report.baseinfo.dal.dataobject.BaseinfoDO;
import cn.iocoder.devlop.module.annual.report.baseinfo.service.BaseinfoService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Aspect
@Component
@Slf4j
public class UpdateBaseinfoAspect {

    @Resource
    private BaseinfoService baseinfoService;

    //    定义切点 , 此处使用 AspectJ 语法 (配置拦截规则)
    @Pointcut(value = "execution(* cn.iocoder.ksy.module.annual.report.*.service.*ServiceImpl.overwrite*(..)) && args(updateReqVO)", argNames = "updateReqVO")
    public void pointcut(Object updateReqVO){

    }




    @After(value = "execution(* cn.iocoder.ksy.module.annual.report.*.service.*ServiceImpl.overwrite*(..)) && args(updateReqVO)", argNames = "updateReqVO")
    public void afterMethodsContainingA(Object updateReqVO) {
        String regno = BeanUtil.getProperty(updateReqVO, "regno");
        String ancheyear = BeanUtil.getProperty(updateReqVO, "ancheyear");
        if (!StrUtil.hasEmpty(regno,ancheyear)){
            BaseinfoDO baseinfo = baseinfoService.getBaseinfo(regno, ancheyear);
            if (ObjUtil.isNull(baseinfo)) {
                baseinfoService.initBaseinfo(regno, ancheyear);
            } else {
                baseinfo.setLastupdatetime(LocalDateTime.now());
                baseinfoService.baseinfoUpdate(baseinfo);
            }
        }

    }
}
